package com.cheejee.goodscenter.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cheejee.goodscenter.dao.SkuBrandPropertiesMapper;
import com.cheejee.goodscenter.module.entity.SkuBrandProperties;
import com.cheejee.goodscenter.service.SkuBrandPropertiesService;
import com.cheejee.goodscenter.service.exception.DataNotExistException;
import com.cheejee.goodscenter.validated.Check;
import com.cheejee.goodscenter.validated.InsertGroup;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @author CARRY
 * @Date 2020年10月29日上午4:00:20
 */
@Service
public class SkuBrandPropertiesServiceImpl implements SkuBrandPropertiesService {

	@Resource
	private SkuBrandPropertiesMapper sbpm;

	@Override
	public SkuBrandProperties queryById(Long id) {

		return sbpm.selectByPrimaryKey(id);
	}

	@Override
	public Page<SkuBrandProperties> querySkuBrandProperties(Long skuId, Integer brandPropertiesTemplateId, String propertiesName, int pageNum, int pageSize) {

		return PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> sbpm.selectBySelective(skuId, brandPropertiesTemplateId, propertiesName));
	}

	@Override
	@Check(message = "新增的SKU品牌属性不能为空", group = InsertGroup.class)
	public int addSkuBrandProperties(SkuBrandProperties properties) {

		return sbpm.insertSelective(properties);
	}
	

	@Override
	@Check(message = "新增的SKU品牌属性不能为空", group = InsertGroup.class)
	public int addSkuBrandProperties(List<SkuBrandProperties> properties) {
		
		return sbpm.insertMany(properties);
	}


	@Override
	public int changeSkuBrandPropertiesValue(Long propertiesId, String propertiesValue){
		checkExist(propertiesId);
		SkuBrandProperties properties = new SkuBrandProperties();
		properties.setId(propertiesId);
		properties.setPropertiesValue(propertiesValue);

		return sbpm.updateByPrimaryKeySelective(properties);
	}

	@Override
	public int deleteSkuBrandProperties(Long... id) {

		return sbpm.deleteById(id);
	}
	
	@Override
	public int deleteSkuBrandPropertiesBySku(Long skuId) {
		return sbpm.deleteBySku(skuId);
	}
	
	private void checkExist(Long id) {
		if(sbpm.selectByPrimaryKey(id) == null) {
			throw new DataNotExistException("SKU品牌属性不存在");
		}
	}
}